package cn.com.bsfit.netty.config.base;

import io.netty.channel.epoll.Native;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 类名 OSInfo
 * 描述 获取当前运行的操作系统信息
 *
 * @author hedonglin
 * @version 1.0
 * @date 2019/9/29 10:16
 */
@SuppressWarnings("all")
public class OSInfo {
    private static String OS = System.getProperty("os.name").toLowerCase();

    private static OSInfo osInstance = new OSInfo();

    private static Logger logger = LoggerFactory.getLogger(OSInfo.class);

    private static Boolean isEpollEnable=null;

    private OsEnum platform;

    private OSInfo() {
    }

    public static synchronized boolean useNettyEpoll() {
        if(isEpollEnable!=null) {
            return isEpollEnable;
        }
        if (isLinux()) {
            try {
                Native.offsetofEpollData();
                isEpollEnable=true;
                return isEpollEnable;
            } catch (UnsatisfiedLinkError error) {
                logger.warn("can not load netty epoll, switch to nio model.");
            }
        }
        isEpollEnable=false;
        return isEpollEnable;
    }

    public static boolean isLinux() {
        return OS.indexOf("linux") >= 0;
    }

    public static boolean isMacOS() {
        return OS.indexOf("mac") >= 0 && OS.indexOf("os") > 0 && OS.indexOf("x") < 0;
    }

    public static boolean isMacOSX() {
        return OS.indexOf("mac") >= 0 && OS.indexOf("os") > 0 && OS.indexOf("x") > 0;
    }

    public static boolean isWindows() {
        return OS.indexOf("windows") >= 0;
    }

    public static boolean isOS2() {
        return OS.indexOf("os/2") >= 0;
    }

    public static boolean isSolaris() {
        return OS.indexOf("solaris") >= 0;
    }

    public static boolean isSunOS() {
        return OS.indexOf("sunos") >= 0;
    }

    public static boolean isMPEiX() {
        return OS.indexOf("mpe/ix") >= 0;
    }

    public static boolean isHPUX() {
        return OS.indexOf("hp-ux") >= 0;
    }

    public static boolean isAix() {
        return OS.indexOf("aix") >= 0;
    }

    public static boolean isOS390() {
        return OS.indexOf("os/390") >= 0;
    }

    public static boolean isFreeBSD() {
        return OS.indexOf("freebsd") >= 0;
    }

    public static boolean isIrix() {
        return OS.indexOf("irix") >= 0;
    }

    public static boolean isDigitalUnix() {
        return OS.indexOf("digital") >= 0 && OS.indexOf("unix") > 0;
    }

    public static boolean isNetWare() {
        return OS.indexOf("netware") >= 0;
    }

    public static boolean isOSF1() {
        return OS.indexOf("osf1") >= 0;
    }

    public static boolean isOpenVMS() {
        return OS.indexOf("openvms") >= 0;
    }

    /**
     * 获取操作系统名字
     *
     * @return 操作系统名
     */
    public static OsEnum getOSname() {
        if (isAix()) {
            osInstance.platform = OsEnum.AIX;
        } else if (isDigitalUnix()) {
            osInstance.platform = OsEnum.Digital_Unix;
        } else if (isFreeBSD()) {
            osInstance.platform = OsEnum.FreeBSD;
        } else if (isHPUX()) {
            osInstance.platform = OsEnum.HP_UX;
        } else if (isIrix()) {
            osInstance.platform = OsEnum.Irix;
        } else if (isLinux()) {
            osInstance.platform = OsEnum.Linux;
        } else if (isMacOS()) {
            osInstance.platform = OsEnum.Mac_OS;
        } else if (isMacOSX()) {
            osInstance.platform = OsEnum.Mac_OS_X;
        } else if (isMPEiX()) {
            osInstance.platform = OsEnum.MPEiX;
        } else if (isNetWare()) {
            osInstance.platform = OsEnum.NetWare_411;
        } else if (isOpenVMS()) {
            osInstance.platform = OsEnum.OpenVMS;
        } else if (isOS2()) {
            osInstance.platform = OsEnum.OS2;
        } else if (isOS390()) {
            osInstance.platform = OsEnum.OS390;
        } else if (isOSF1()) {
            osInstance.platform = OsEnum.OSF1;
        } else if (isSolaris()) {
            osInstance.platform = OsEnum.Solaris;
        } else if (isSunOS()) {
            osInstance.platform = OsEnum.SunOS;
        } else if (isWindows()) {
            osInstance.platform = OsEnum.Windows;
        } else {
            osInstance.platform = OsEnum.Others;
        }
        return osInstance.platform;
    }
}
